package com.cci.kangdao.common.push;

import com.cci.kangdao.common.push.entity.umeng.Android;
import com.cci.kangdao.common.push.entity.umeng.IOS;
import com.cci.kangdao.common.push.entity.umeng.Umeng;
import com.cci.kangdao.common.push.umengsdk.AndroidNotification;
import com.cci.kangdao.common.push.umengsdk.PushClient;
import com.cci.kangdao.common.push.umengsdk.android.AndroidBroadcast;
import com.cci.kangdao.common.push.umengsdk.android.AndroidGroupcast;
import com.cci.kangdao.common.push.umengsdk.android.AndroidUnicast;
import com.cci.kangdao.common.push.umengsdk.ios.IOSBroadcast;
import com.cci.kangdao.common.push.umengsdk.ios.IOSGroupcast;
import com.cci.kangdao.common.push.umengsdk.ios.IOSUnicast;
import com.gexin.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.PushResult;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2022/4/7 11:57
 * @version: V1.0
 **/
public class UmengConfig implements PushService {
    private static Umeng umeng = new Umeng();

    private PushClient client = new PushClient();

    static {
        init();
    }

    private static void init() {
        Properties properties = new Properties();
        String type = "";
        try {
            InputStream inputStream = UmengConfig.class.getClassLoader().getResourceAsStream("projectconfig.properties");
            properties.load(inputStream);
            inputStream.close();
            Android android = new Android();
            android.setAppKey(properties.getProperty("umeng.android.appKey"));
            android.setAppMasterSecret(properties.getProperty("umeng.android.appMasterSecret"));
            android.setUmengMessageSecret(properties.getProperty("umeng.android.umengMessageSecret"));
            IOS ios = new IOS();
            ios.setAppKey(properties.getProperty("umeng.ios.appKey"));
            ios.setAppMasterSecret(properties.getProperty("umeng.ios.appMasterSecret"));
            umeng.setAndroid(android);
            umeng.setIos(ios);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public boolean push2Single(String cId, String title, String content, int transmissionType, String transmissionContent) {
        this.AndroidandIosUnicast(cId, title, content, transmissionType, transmissionContent);
        return true;
    }

    @Override
    public boolean push2Single(String cId, String content, String transmissionContent) {
        return push2Single(cId, null, content, 2, transmissionContent);
    }


    //安卓和ios 两个同时发送
    public void AndroidandIosUnicast(String cId, String title, String content, int transmissionType, String transmissionContent) {
        if (StringUtils.isNotBlank(cId)) {
            if (cId.length() <= 44) {
                try {
                    sendAndroidUnicast(cId, title, content, transmissionType, transmissionContent);//  安卓
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    sendIOSUnicast(cId, title, content, transmissionType, transmissionContent); //  ios
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //  安卓  单播
    public void sendAndroidUnicast(String cId, String title, String content, int transmissionType, String transmissionContent) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(umeng.getAndroid().getAppKey(), umeng.getAndroid().getAppMasterSecret());
        // TODO Set your device token
        unicast.setDeviceToken(cId);
        unicast.setTicker(content);
        unicast.setTitle(StringUtils.isEmpty(title) ? "e路康明斯" : title);
        unicast.setText(content);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        unicast.setProductionMode();
        // Set customized fields
        unicast.setExtraField("test", "helloworld");
        unicast.setChannelActivity("your channel activity");
        unicast.setChannelProperties("abc");
        client.send(unicast);
    }

    //  ios  单播
    public void sendIOSUnicast(String cId, String title, String content, int transmissionType, String transmissionContent) throws Exception {
        IOSUnicast unicast = new IOSUnicast(umeng.getIos().getAppKey(), umeng.getIos().getAppMasterSecret());
        // TODO Set your device token
        unicast.setDeviceToken(cId);
        //alert值设置为字符串
        //unicast.setAlert("IOS 单播测试");
        //alert的值设置为字典
        unicast.setAlert(title, title, content);
        unicast.setBadge(0);
        unicast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
//        unicast.setTestMode();
        unicast.setProductionMode();
        // Set customized fields
        unicast.setCustomizedField("test", "helloworld");
        client.send(unicast);
    }

    @Override
    public IPushResult pushMessageToApp(String content, int transmissionType, String transmissionContent) {

        return this.AndroidandIosGroupcast(content, transmissionType, transmissionContent);
    }

    //    安卓和iOS 同时  广播
    public IPushResult AndroidandIosGroupcast(String content, int transmissionType, String transmissionContent) {
        IPushResult iPushResult = new PushResult();
        try {
            sendAndroidBroadcast(content, transmissionType, transmissionContent); //  ios
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            sendIOSBroadcast(content, transmissionType, transmissionContent); //  ios
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iPushResult;
    }

    //     安卓  群播
    //广播
    public void sendAndroidBroadcast(String content, int transmissionType, String transmissionContent) throws Exception {
        AndroidBroadcast broadcast = new AndroidBroadcast(umeng.getAndroid().getAppKey(), umeng.getAndroid().getAppMasterSecret());
        broadcast.setTicker(content);
        broadcast.setTitle("e路康明斯");
        broadcast.setText(transmissionContent);
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        broadcast.setProductionMode();
        // Set customized fields
        broadcast.setExtraField("test", "helloworld");
        //厂商通道相关参数
        broadcast.setChannelActivity("your channel activity");
        broadcast.setChannelProperties("abc");
        client.send(broadcast);
    }

    //     ios  群播  广播
    public void sendIOSBroadcast(String content, int transmissionType, String transmissionContent) throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(umeng.getIos().getAppKey(), umeng.getIos().getAppMasterSecret());
        //alert值设置为字符串
        //broadcast.setAlert("IOS 广播测试");
        //alert的值设置为字典
        broadcast.setAlert("e路康明斯", content, transmissionContent);
        broadcast.setBadge(0);
        broadcast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
//        broadcast.setTestMode();
        broadcast.setProductionMode();
        // Set customized fields
        broadcast.setCustomizedField("test", "helloworld");
        client.send(broadcast);
    }

}
